classdef OCCLUDER

    properties

        y   = 0
        h   = 50
        rgb = [0 0 0]
        
        X
        W
        End
        
        Wbase
        Xbase

        Xframe
        Endframe
        
        Xrand
        
        Endbase

        frames
        time
        
        startw4 = [-6,-2,2,6]     % starting positions for occW of 4
        startw8 = [-6,-4,-2,4]    % starting positions for occW of 8
        startw3 = [-6,-2,2,6.5]   % starting positions for occW of 3
        startw6 = [-6,-3,0,5]     % starting positions for occW of 6
        
    end

    methods

        function self = OCCLUDER(exp)
            % Defines a set of occluders. The input is a instance of
            % eyetrackexp
            
            % W's are 3 or 6
            
            self.Wbase = (round(rand(exp.num_blocks,exp.num_trials))+1).*3; 
            self.Xbase = zeros(size(self.Wbase));
            self.Xrand = ceil(rand(exp.num_blocks,exp.num_trials)*4);

            for b=1:exp.num_blocks;
                for t=1:exp.num_trials;
                    
                    if self.Wbase(b,t) == 3  %% CHANGE THIS IF WBASE CHANGED %%
                        self.Xbase(b,t) = self.startw3(self.Xrand(b,t));
                    else
                        self.Xbase(b,t) = self.startw6(self.Xrand(b,t));
                    end
                end      
            end
            
            % creates matrix of occluder start points according to occ width
            
            self.Endbase = self.Wbase + self.Xbase; % where occluders end
            self.X = self.Xbase.*((exp.scn.w/16)*(-exp.fix.x));
            % Converts occ sizes to SPEM path in pixels
            self.W = self.Wbase.*((exp.scn.w/16)*(-exp.fix.x)); 
            
            self.End    = self.W + self.X;
            self.Xframe = zeros(size(self.W)); % frame at which occluder starts

            self.Endframe = zeros(size(self.W)); % frame at which occluder ends
            
            % for b=1:num_blocks;
            %     t = randi([1,num_trials],1,1); % creates at least one 'end occluder' per block
            %     occEndbase(b,t) = 8;
            %     occXbase(b,t) = 8 - occWbase(b,t);
            %     occWbase(b,t) = occWbase(b,t) + 1;
            % % ensures that occluders which finish at position '8' occlude whole path
            % end
            
            for b=1:exp.num_blocks;
                for t=1:exp.num_trials;
                    self.Xframe(b,t) = round(exp.FpC(b,t)*...
                        acos(self.X(b,t)/(exp.fix.x*exp.scn.w/2))/(2*pi));
                end
            end
            
            for b=1:exp.num_blocks;
                for t=1:exp.num_trials;
                    if self.Endbase(b,t) >= 8  % can't use formula below as end occluders overhang stim path
                        self.Endframe(b,t) = exp.FpC(b,t)/2;
                    else
                        self.Endframe(b,t) = round(exp.FpC(b,t)*acos(...
                            self.End(b,t)/(exp.fix.x*exp.scn.w/2))/(2*pi));
                    end
                end
            end
            
            self.frames = self.Endframe - self.Xframe;
            self.time   = self.frames./60; % time stimulus is occluded in seconds
            
        end
    end
end